<?php

namespace App\Model;

use PhalApi\Model\DataModel;



class IdcardWater extends DataModel
{
    //对单位人员进行排序
    public $tableName = 'phalapi_user';
    protected function getTableName($id)
    {
        return $this->tableName;
    }


    //计算出开头
    public function Start($username)
    {
        $this->tableName = 'phalapi_user';
        $r = $this->getORM()->where('username', $username)->select('water_order')->fetchOne();
        return $r['water_order'];
    }

    //计算出当天第几个
    public function Water($date)
    {
        $unit = $date['unit'];
        $username = $date['checker'];

        $this->tableName = 'idcard_id_int_format';
        //当天时间
        $date = date("Ymd", time());
        //如果当天已经有了数据,就开始增加
        $r = $this->getORM()->where(array(
                                            'date' => $date,
                                            'username' => $username,
                                            'unit' => $unit,
                                        ))->fetchOne();
        if ( $r ){
            $r['water'] = $r['water'] + 1;
            $this->getORM()->where("id", $r['id'])->update($r);
        }else{
            $r = $this->getORM()->insert(array(
                'username' => $username,
                'unit' => $unit,
                'date' => $date,
                'water' => 1,
            ));
        }
        $res = $this->Start($username) . "-" . $r['water'];
        return $res;
    }


    //计算出当天第几个
    public function Unit_Water($date)
    {
        $unit = $date['unit'];
        $username = $date['checker'];
        $from_unit_code = $date['from_unit_code'];

        $this->tableName = 'idcard_id_int_format';
        //当天时间
        $date = date("Ymd", time());
        //如果当天已经有了数据,就开始增加
        $r = $this->getORM()->where(
            array(
                    'unit' => $unit,
                    'from_unit_code' => $from_unit_code,
                ))->fetchOne();
                
        if ( $r ){
            $r['water'] = $r['water'] + 1;
            $this->getORM()->where("id", $r['id'])->update($r);
        }else{
            $r = $this->getORM()->insert(array(
                'username' => $username,
                'unit' => $unit,
                'from_unit_code' => $from_unit_code,
                'water' => 1,
                'create_time' => date("Y-m-d H:i:s", time()),
            ));
        }
        $res = $from_unit_code . "-" . $r['water'];
        return $res;
    }
}
